Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow references to files outside js/ and css/ dirs #26

Merged
merged 19 commits into from
Mar 31, 2024

Conversation

timriley
Copy link
Member

@timriley timriley commented Mar 19, 2024

Stop marking the directories aside from js/ and css/ as "external". This allows the files inside those other directories to be referenced from within JS/CSS files and properly bundled by esbuild.

This incorporates @krzykamil's (excellent!) suggestion in #27, which uses an onLoad callback to track referenced files so that we can exclude them from separate handling when we copy over the non-referenced asset files.

Specific changes:

  • Stop marking any directories as external (in esbuild.js)
  • Move findExternalDirectories from esbuild.js to esbuild-plugin.js as extraAssetDirs (a more appropriate name now that nothing is marked as external), and call this directly when determining which directories we should manually copy via processAssetDirectory.
  • Introduce an onLoad callback to esbuild-plugin.js and use this to track any files that esbuild itself loads.
  • Use this to skip already-loaded files inside processAssetDirectory, which means there will be no double-processing of the files that are referenced from JS/CSS.
  • Then when handling esbuild's outputs (i.e. all the loaded files) to include in the manifest, make sure to use the output's original input filename for the manifest key, to avoid collisions and to keep those manifest keys consistent with non-loaded files that are copied across as part of processAssetDirectory.
  • Plus a range of other refactors/tidyings

Thanks also to @svoop for the great initial bug report (in #24 as well as the ensuing forum conversation)!

Fixes #24.

This allows the files inside those other directories to be referenced from within JS/CSS files and properly bundled by esbuild.
@krzykamil
Copy link
Contributor

#27 my take on the TODO @timriley

Use an onLoad callback to track those references.

This incorporates the approach from #27.
@timriley timriley marked this pull request as ready for review March 25, 2024 00:58
@timriley
Copy link
Member Author

@krzykamil your idea is brilliant! I've just incorporated it into this PR. Could you please test things again on your app, using this branch?

@krzykamil
Copy link
Contributor

It looks fine, fonts are not duplicated and everything is correctly nested in my public/assets/_main

@timriley
Copy link
Member Author

Just noting that using this branch with an app with slice assets fails to include the slice's JS/CSS files in the manifest. See this forum post for details.

This is a bug and would be a regression. I'll address this before wrapping up this PR (and hopefully capture this in a test somehow).

@timriley timriley marked this pull request as draft March 27, 2024 13:07
This can just return an array now.

Also move it to the bottom of the file so it doesn’t get in the way of the more important things.
@timriley timriley force-pushed the allow-references-to-files-outside-js-css-dirs branch from 0627aa2 to 3b29f91 Compare March 29, 2024 00:49
@timriley timriley changed the title Don't mark non-{js,css} dirs as external Allow references to files outside js/ and css/ dirs Mar 29, 2024
@timriley timriley marked this pull request as ready for review March 29, 2024 01:22
Call it extraAssetDirectories, which better reflects its purpose now, since we’re no longer marking any directories as “external”.

Also, move this function to the bottom of the file to keep the mainline code easier to follow.
This means that if there is a “images/a/a.png” and a “images/b/b.png”, they’ll both have consistent manifest keys (“a/a.png” and “b/b.png” respectively), even in cases where esbuild is directly bundling one of the two files.
@timriley timriley force-pushed the allow-references-to-files-outside-js-css-dirs branch from bc46ffd to b53b416 Compare March 29, 2024 05:26
Make this more consistent with esbuild’s name for this process.
This no longer seems necessary.
Make it clearer that the “path” is just the “dir” but a full path.
Make it clearer how we handle these in full by copying the assets and then populating their entries in the manifest immediately after.
@timriley timriley self-assigned this Mar 31, 2024
@timriley timriley added the bug Something isn't working label Mar 31, 2024
@timriley timriley added this to the v2.1.1 milestone Mar 31, 2024
@timriley timriley merged commit e784a6b into main Mar 31, 2024
2 checks passed
@timriley timriley deleted the allow-references-to-files-outside-js-css-dirs branch March 31, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect paths after asset compile
2 participants